package com.ls.lserp.modules.inventoryControl.service;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.modules.inventoryControl.dao.InventoryDao;
import com.ls.lserp.modules.inventoryControl.entity.InventoryEntity;
import com.ls.lserp.modules.inventoryControl.entity.OginzationEntity;
import com.ls.lserp.modules.order.dao.OrderMaterielDao;
import com.ls.lserp.modules.order.entity.OrderMateriel;

/**
 * @ClassName: InventoryService
 * @Description: 库存预警 新建 删除 修改
 * 
 * @author 于辉
 * @date 2017年03月13日
 * 
 */

@Service
@Transactional(readOnly = false)
public class InventoryService extends CrudService<InventoryDao, InventoryEntity> {
	@Autowired
	// 分类方法接口
	private InventoryDao inventoryDao;
	// 分类方法接口
	@Autowired
	private OrderMaterielDao orderMaterielDao;
	/**
	 * @Title: findList
	 * @Description: 一览查询
	 * @param page
	 * @param InventoryEntity
	 * @return Page<InventoryEntity>
	 */
	public Page<InventoryEntity> findList(Page<InventoryEntity> page, InventoryEntity inventoryEntity) {
		if ("0".equals(inventoryEntity.getMaterialStatus())) {
			inventoryEntity.setMaterialStatus("");
		}else if("1".equals(inventoryEntity.getMaterialStatus())){
			inventoryEntity.setMaterialStatus(ResourcesUtil.getConfig("inventoryAlert_00"));
		}else if("2".equals(inventoryEntity.getMaterialStatus())){
			inventoryEntity.setMaterialStatus(ResourcesUtil.getConfig("inventoryAlert_10"));
		}else if("3".equals(inventoryEntity.getMaterialStatus())){
			inventoryEntity.setMaterialStatus(ResourcesUtil.getConfig("inventoryAlert_20"));
		}
		// 设置分页参数
		inventoryEntity.setPage(page);
		// 执行分页查询
		page.setList(inventoryDao.findList(inventoryEntity));
		return page;
	}

	/**
	 * @Title: 获取供货商信息
	 * @Description: 一览查询
	 * @param materialId
	 * @return List<OrderMateriel>
	 */
	public List<OrderMateriel> findVenderList(OrderMateriel paramVender) {
		return orderMaterielDao.findVenderlList(paramVender);
	}
	
	/**
	 * @Title: findOgnizationList
	 * @Description: 供货商一览
	 * @param InventoryEntity
	 */
	public List<OginzationEntity> findOgnizationList(InventoryEntity inventoryEntity) {
		List<OginzationEntity> oginzationList = null;
		// 供货商查询
		oginzationList = inventoryDao.findOgnizationList(inventoryEntity);
		return oginzationList;
	}

	/**
	 * @Title: updateInventory
	 * @Description: 预警画面更新
	 * @param InventoryEntity
	 */
	public void insertInventory(InventoryEntity inventoryEntity) throws SQLException{
		//删除数据
		inventoryDao.delete(inventoryEntity);
		if (inventoryEntity.getInventoryList() != null && inventoryEntity.getInventoryList().size()>0) {
			for (int i = 0; i < inventoryEntity.getInventoryList().size(); i++) {
				if (inventoryEntity.getInventoryList().get(i) != null) {
					inventoryEntity.getInventoryList().get(i).preInsert();
					inventoryEntity.getInventoryList().get(i).setAlertId(inventoryEntity.getAlertId());
					inventoryEntity.getInventoryList().get(i).setMaterialId(inventoryEntity.getMaterialId());
					inventoryEntity.getInventoryList().get(i).setMaterialReserverKbn(ResourcesUtil.getConfig("reserverAlert_10"));
					// 插入数据
					inventoryDao.insert(inventoryEntity.getInventoryList().get(i));
				}
			}
		}
	}
	/**
	 * @Title: findReservserAlertDetail
	 * @Description: 获取预警明细数据
	 * @param InventoryEntity
	 */
	public List<InventoryEntity> findReservserAlertDetail(InventoryEntity inventoryEntity) {
		return inventoryDao.findReservserAlertDetail(inventoryEntity);
	}
	
	/**
	 * @Title: 通过面料ID获取已预订库存
	 * @Description: 一览查询
	 * @param materialId
	 * @return List<OrderMateriel>
	 */
	public BigDecimal sumMaterialQuantity(OrderMateriel orderMateriel) {
		BigDecimal unusableMQuantity = orderMaterielDao.sumMaterialQuantity(orderMateriel);
		if (unusableMQuantity==null) {
			unusableMQuantity = new BigDecimal(0);
		}
		return unusableMQuantity;
	}
	
	/**
	 * @Title: updateInventory
	 * @Description: 预警画面更新
	 * @param InventoryEntity
	 */
	public void deleteAlertReserve(String alertId) throws SQLException{
		//删除数据
		inventoryDao.deleteAlertReserve(alertId);
	}
}
